public static void execute (String[] commands, String[] headers)
{
// build commands into new java file
try
{
FileWriter fstream = new FileWriter("Example.java");
BufferedWriter out = new BufferedWriter(fstream);
out.write("");
for (String header : headers) out.append(header);
out.append("class Example { public static void main(String args[]) { ");
for (String cmd : commands) out.append(cmd);
out.append(" } }");
out.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
// set path, compile, & run
try
{
Process tr = Runtime.getRuntime().exec(
new String[]{ "java -cp .",
"javac Example.java",
"java Example" } );
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}